package website.seamonster.ethegg.testdelay;

import java.util.concurrent.*;

/**
 * Created by 罗海量
 * 2018-07-17 16:26
 */
@lombok.extern.slf4j.Slf4j
public class DelayQueueTest {
    public static void main(String[] args) {
        System.out.println(test1());
    }

    private static int test1(){
        // 创建延时队列
        DelayQueue<Message> queue = new DelayQueue<Message>();
        // 添加延时消息,m1 延时3s
        Message m1 = new Message(1, "world", 3000);
        // 添加延时消息,m2 延时10s
        Message m2 = new Message(2, "hello", 10000);
        //将延时消息放到延时队列中
        queue.offer(m2);
        queue.offer(m1);
        // 启动消费线程 消费添加到延时队列中的消息，前提是任务到了延期时间
        System.out.println('1');
        ExecutorService exec = Executors.newFixedThreadPool(100);
        System.out.println('2');
        exec.execute(new Consumer(queue));
        System.out.println('3');
        exec.shutdown();
        System.out.println('4');
        return 0;
    }
}
